﻿using NfsCommon.DbHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NfsDAL
{
  public  class ShareDAL
    {
        public ShareDAL()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("id", "t_share"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_share");
			strSql.Append(" where id="+id+" ");
			return DbHelperMySQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(NfsModel.share model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.level_id != null)
			{
				strSql1.Append("level_id,");
				strSql2.Append(""+model.level_id+",");
			}
            if (model.agency_id != null)
            {
                strSql1.Append("agency_id,");
                strSql2.Append("" + model.agency_id + ",");
            }
            if (model.category_id != null)
            {
                strSql1.Append("category_id,");
                strSql2.Append("" + model.category_id + ",");
            }
			if (model.parent_id != null)
			{
				strSql1.Append("parent_id,");
				strSql2.Append(""+model.parent_id+",");
			}
			if (model.nonceStr != null)
			{
				strSql1.Append("nonceStr,");
				strSql2.Append("'"+model.nonceStr+"',");
			}
            if (model.state != null)
            {
                strSql1.Append("state,");
                strSql2.Append("" + model.state + ",");
            }
			if (model.valid != null)
			{
				strSql1.Append("valid,");
				strSql2.Append(""+model.valid+",");
			}
			if (model.time != null)
			{
				strSql1.Append("time,");
				strSql2.Append("'"+model.time+"',");
			}
            if (model.level_name != null)
            {
                strSql1.Append("level_name,");
                strSql2.Append("'" + model.level_name + "',");
            }
			strSql.Append("insert into t_share(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(NfsModel.share model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_share set ");
			if (model.level_id != null)
			{
				strSql.Append("level_id="+model.level_id+",");
			}
			
			if (model.parent_id != null)
			{
				strSql.Append("parent_id="+model.parent_id+",");
			}
            if (model.agency_id != null)
            {
                strSql.Append("agency_id=" + model.agency_id + ",");
            } 
            if (model.category_id != null)
            {
                strSql.Append("category_id=" + model.category_id + ",");
            }
			if (model.nonceStr != null)
			{
				strSql.Append("nonceStr='"+model.nonceStr+"',");
			}
			
            if (model.state != null)
            {
                strSql.Append("state=" + model.state + ",");
            }
			if (model.valid != null)
			{
				strSql.Append("valid="+model.valid+",");
			}
			
			if (model.time != null)
			{
				strSql.Append("time='"+model.time+"',");
			}
            if (model.level_name != null)
            {
                strSql.Append("level_name='" + model.level_name + "',");
            }
			
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id="+ model.id+"");
			int rowsAffected=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_share ");
			strSql.Append(" where id="+id+"" );
			int rowsAffected=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_share ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public NfsModel.share GetModel(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
            strSql.Append(" id,level_id,agency_id,category_id,level_name,parent_id,nonceStr,state,valid,time ");
			strSql.Append(" from t_share ");
			strSql.Append(" where id="+id+"" );
            NfsModel.share model = new NfsModel.share();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public NfsModel.share DataRowToModel(DataRow row)
		{
            NfsModel.share model = new NfsModel.share();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["level_id"]!=null && row["level_id"].ToString()!="")
				{
					model.level_id=int.Parse(row["level_id"].ToString());
				}
                if (row["agency_id"] != null && row["agency_id"].ToString() != "")
                {
                    model.agency_id = int.Parse(row["agency_id"].ToString());
                }
                if (row["category_id"] != null && row["category_id"].ToString() != "")
                {
                    model.category_id = int.Parse(row["category_id"].ToString());
                }
				if(row["parent_id"]!=null && row["parent_id"].ToString()!="")
				{
					model.parent_id=int.Parse(row["parent_id"].ToString());
				}
				if(row["nonceStr"]!=null)
				{
					model.nonceStr=row["nonceStr"].ToString();
				}
                if (row["state"] != null && row["state"].ToString() != "")
                {
                    model.state = int.Parse(row["state"].ToString());
                }
				if(row["valid"]!=null && row["valid"].ToString()!="")
				{
					model.valid=int.Parse(row["valid"].ToString());
				}
				if(row["time"]!=null && row["time"].ToString()!="")
				{
					model.time=DateTime.Parse(row["time"].ToString());
                }
                if (row["level_name"] != null)
                {
                    model.level_name = row["level_name"].ToString();
                }


			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,level_id,agency_id,category_id,level_name,parent_id,nonceStr,state,valid,time ");
			strSql.Append(" FROM t_share ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_share ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from t_share T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx
        /// <summary>
        /// 用随机戳获取信息
        /// </summary>
        public NfsModel.share GetModel_nonceStr(string nonceStr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" id,level_id,agency_id,category_id,level_name,parent_id,nonceStr,state,valid,time ");
            strSql.Append(" from t_share ");
            strSql.Append(" where nonceStr='" + nonceStr + "' and valid=1");
            NfsModel.share model = new NfsModel.share();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        #region 模糊查询分页
        public DataTable GetShareBycondtion(int pageindex, int pagesize, string name,int state,int parent_id)
        {
            int startindex = (pageindex - 1) * pagesize;
            string tem=string.Empty;
            string strSql = @"select top "+pagesize+" * from (select row_number() over(order by sh.id desc) as rownumber, sh.*,ag.name as agencyName,ag.sq_img,le.name as levelName,ca.name as cateName  from t_share as  sh left join t_agency as ag on sh.agency_id=ag.id left join t_level as le on le.id=ag.level_id LEFT join t_category as ca on ca.id=ag.category_id where sh.valid=1 {0} ) as a where 1=1 and a.rownumber>"+startindex+" "; 
            if (name!=null && name != "")
            {
                tem += " and name like'%" + name + "%'";
            }

            tem += " and sh.state=" + state + "";
            tem += " and sh.parent_id=" + parent_id + "";
            strSql = string.Format(strSql,tem);
            return DbHelperMySQL.Query(strSql).Tables[0];
        }
#endregion
        #region 根据经销id修改信息审核状态
      
        public bool Update_state(int state,int agency_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_share set ");
            if (state != null)
            {
                strSql.Append("state=" + state + ",");
            }

            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where agency_id=" + agency_id + "");
            int rowsAffected = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion 
        #endregion  MethodEx
    }
}

